import { DD_SYSTEM } from '@/constants/api-pre';
import { request } from '@/service/request';

export interface FileClientConfig {
  basePath: string;
  host?: string;
  port?: number;
  username?: string;
  password?: string;
  mode?: string;
  endpoint?: string;
  bucket?: string;
  accessKey?: string;
  accessSecret?: string;
  domain?: string;
}

/** 文件配置对象实体 */
export type FileConfig = Api.Common.CommonRecord<{
  /** 配置名 */
  name: string;
  /** 存储器 */
  storage: string | null;
  /** 是否为主配置 */
  master: boolean;
  /** 存储配置 */
  config: FileClientConfig;
  /** 备注 */
  remark: string;
}>;

export type FileConfigList = Api.Common.PaginatingQueryRecord<FileConfig>;

/** 文件配置查询参数 */
export type FileConfigSearchParams = CommonType.RecordNullable<
  Pick<FileConfig, 'name' | 'storage'> &
    Api.Common.CommonSearchParams & {
      /** 创建时间 */
      createTime: null;
    }
>;

export type FileConfigEditAndCreateParams = CommonType.RecordNullable<
  Pick<FileConfig, 'name' | 'storage' | 'master' | 'config' | 'remark'>
>;

/** 获取文件配置表单 URL */
export function getFetchFileConfigUrl(id: number) {
  return `${DD_SYSTEM}/file-config/${id}/form`;
}

/** 获取文件配置列表 */
export function fetchGetFileConfigList(params?: FileConfigSearchParams) {
  return request<FileConfigList>({
    url: `${DD_SYSTEM}/file-config/page`,
    method: 'get',
    params
  });
}

/**
 * 新增文件配置
 *
 * @param data
 */
export function createFileConfig(data: FileConfigEditAndCreateParams) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/file-config`,
    method: 'post',
    data
  });
}

/**
 * 编辑文件配置
 *
 * @param id
 * @param data
 */
export function updateFileConfig(id: number, data?: FileConfigEditAndCreateParams) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/file-config/${id}`,
    method: 'put',
    data
  });
}

/**
 * @param id 更新文件配置为主配置
 * @returns
 */
export const updateFileConfigMaster = (id: number) => {
  return request<Boolean>({
    url: `${DD_SYSTEM}/file-config/update-master/${id}`,
    method: 'patch'
  });
};

/**
 * 删除文件配置
 *
 * @param id
 */
export function deleteFileConfig(id: string) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/file-config/${id}`,
    method: 'delete'
  });
}

/**
 * 测试文件配置
 *
 * @param id
 * @returns
 */
export const testFileConfig = (id: number) => {
  return request<string>({
    url: `${DD_SYSTEM}/file-config/test/${id}`,
    method: 'get'
  });
};
